<?php
    define('CONFIG_FILE',true);
    include '../config.php';
    
    define('DB_FILE',true);
    require_once '../Tool/DB/SqlDBManager.class.php';
    require_once '../Tool/Common/PrepareInput.php';

    session_start();
    if (empty($_SESSION["userType"])) {
        $url="../Index.php";
		include_once '../Tool/Common/Redirect.php';
		exit();
    }

    if($_SESSION["userType"]=="Therapy Assistant"){
        $url="../Index.php";
		include_once '../Tool/Common/Redirect.php';
		exit();
    }

    $UpdateUsername=$UpdateStaffName=$UpdateContactNo=$UpdateUserType=$UpdateCentreName=$UpdateEmail="";

    if(isset($_REQUEST['UpdateUsername'])) { 
        $UpdateUsername=prepareInput($_REQUEST['UpdateUsername']); 
    }
    if(isset($_REQUEST['UpdateStaffName'])) { 
        $UpdateStaffName=prepareInput($_REQUEST['UpdateStaffName']); 
    }
    if(isset($_REQUEST['UpdateContactNo'])) { 
        $UpdateContactNo=prepareInput($_REQUEST['UpdateContactNo']); 
    }
    if(isset($_REQUEST['UpdateUserType'])) { 
        $UpdateUserType=prepareInput($_REQUEST['UpdateUserType']); 
    }
    if(isset($_REQUEST['UpdateCentreName'])) { 
        $UpdateCentreName=prepareInput($_REQUEST['UpdateCentreName']); 
    }
    if(isset($_REQUEST['UpdateEmail'])) { 
        $UpdateEmail=prepareInput($_REQUEST['UpdateEmail']); 
    }

    $result=array();

    $result['flagUserName']=0;
    $result['flagStaffName']=0;
    $result['flagContactNo']=0;
    $result['flagUserType']=0;
    $result['flagCentreName']=0;
    $result['flagEmailFormat']=0;  
    $result['flagEmailExist']=1;

    if(preg_match("/^[0-9A-Za-z ]*$/",$UpdateUsername)&&!empty($UpdateUsername)){
        $result['flagUserName']=1;
    }
    if(preg_match("/^[A-Za-z-. ]*$/",$UpdateStaffName)&&!empty($UpdateStaffName)){
        $result['flagStaffName']=1;
    }
    if(preg_match("/^[689][0-9]{7}$/",$UpdateContactNo)&&!empty($UpdateContactNo)){
        $result['flagContactNo']=1;
    }
    if(preg_match("/^[A-Za-z ]*$/",$UpdateUserType)&&!empty($UpdateUserType)){
        $result['flagUserType']=1;
    }
    if(preg_match("/^[0-9A-Za-z() ]*$/",$UpdateCentreName)&&!empty($UpdateCentreName)){
        $result['flagCentreName']=1;
    }

    //Validate Email Format
    if(preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$UpdateEmail)&&!empty($UpdateEmail)){
        $result['flagEmailFormat']=1;  
    }

    //Validate Email exist or not
    $sqlDBManager = new SqlDBManager();
    
    $sql="SELECT Email FROM user WHERE Username=?";
    $parameters=array($UpdateUsername);
    $res=$sqlDBManager->queryRow($sql,$parameters);

    //Indicates email already existed 
    if(!empty($res)){
        $existedEmail=$res['Email'];
        if($existedEmail==$UpdateEmail){
            $result['flagEmailExist']=1;  
        }else{
            $sql="Select Email FROM User";
            $res=$sqlDBManager->queryRows($sql);
            if(!empty($res)){
                for($i=0;$i<count($res);$i++){
                    $result['flagEmailExist']=0;
                    if($res[$i]['Email']==$UpdateEmail){
                        break;
                    }else{
                        $result['flagEmailExist']=1; 
                    }
                }
            }   
        }   
    }else{
        $sql="Select Email FROM User";
        $res=$sqlDBManager->queryRows($sql);
        if(!empty($res)){
            for($i=0;$i<count($res);$i++){
                $result['flagEmailExist']=0;
                if($res[$i]['Email']==$UpdateEmail){
                    break;
                }else{
                    $result['flagEmailExist']=1; 
                }
            }
        }     
    }  

    echo json_encode($result);
?>